package com.wakala.employment.fragment.recruit;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.enums.PopupAnimation;
import com.wakala.employment.R;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.databinding.FragmentResumeRefreshBinding;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.textview.label.LabelTextView;
import com.xuexiang.xutil.display.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ResumeRefreshFragment#} factory method to
 * create an instance of this fragment.
 */
@Page(anim = CoreAnim.none)
public class ResumeRefreshFragment extends BaseFragment<FragmentResumeRefreshBinding> implements View.OnClickListener {

    private static String TAG = ResumeRefreshFragment.class.getSimpleName();

    private Handler mHandler = new Handler(Looper.getMainLooper());

    private List<LinearLayout> mLinerViewList = new ArrayList<>();
    private List<TextView> mTextViewList = new ArrayList<>();
    private List<TextView> mTextViewList2 = new ArrayList<>();


    @NonNull
    @Override
    protected FragmentResumeRefreshBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentResumeRefreshBinding.inflate(inflater, container, attachToRoot);
    }

    @Override
    protected TitleBar initTitle() {
        return super.initTitle().setTitle("简历刷新");
    }

    @Override
    protected void initListeners() {
        super.initListeners();
        //
        binding.ssBtnRefresh.setOnClickListener(this);
        //
        binding.tvLabel3.setOnClickListener(this);
        binding.tvLabel7.setOnClickListener(this);
        binding.tvLabel15.setOnClickListener(this);
        binding.tvLabel30.setOnClickListener(this);
        //
        binding.llSmartRefresh.setOnClickListener(this);
        binding.llSmartRefresh.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // 按下事件
                        binding.llSmartRefresh.setBackgroundColor(getResources().getColor(R.color.color_label_view_active));
                        break;
                    case MotionEvent.ACTION_UP:
                        // 抬起事件
                        binding.llSmartRefresh.setBackground(getResources().getDrawable(R.drawable.shape_round_blue_tr_3dp));
                        break;
                    case MotionEvent.ACTION_MOVE:
                        // 移动事件
                        break;
                    default:
                        break;
                }
                // 返回true表示事件已被处理，不再传递。返回false则事件继续传递
                return false;
            }
        });
    }


    @Override
    protected void initViews() {

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_label3) {
            activeLableView(binding.tvLabel3);
            disableLableView(binding.tvLabel7);
            disableLableView(binding.tvLabel15);
            disableLableView(binding.tvLabel30);
        } else if (id == R.id.tv_label7) {
            disableLableView(binding.tvLabel3);
            activeLableView(binding.tvLabel7);
            disableLableView(binding.tvLabel15);
            disableLableView(binding.tvLabel30);
        } else if (id == R.id.tv_label15) {
            disableLableView(binding.tvLabel3);
            disableLableView(binding.tvLabel7);
            activeLableView(binding.tvLabel15);
            disableLableView(binding.tvLabel30);
        } else if (id == R.id.tv_label30) {
            disableLableView(binding.tvLabel3);
            disableLableView(binding.tvLabel7);
            disableLableView(binding.tvLabel15);
            activeLableView(binding.tvLabel30);
        } else if (id == R.id.ll_smart_refresh) {
            new XPopup.Builder(getContext()).asCustom(new CenterPopupView(requireContext()) {
                @Override
                protected int getImplLayoutId() {
                    return R.layout.dialog_alter;
                }

                @Override
                protected void onCreate() {
                    super.onCreate();
                    findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismiss();
                        }
                    });
                    TextView title = findViewById(R.id.txt_dialog_content);
                    title.setText("尊敬的用户，河狸帮给新用户赠送免费体验会员功能权益，体验期间，简历每日自动刷新一次，不需要您花费积分。");
                }
            }).show();
            /*new XPopup.Builder(getContext())
                    .popupAnimation(PopupAnimation.TranslateAlphaFromBottom)  //设置动画
                    .dismissOnTouchOutside(true)  // 点击外面消失
                    .maxWidth(ScreenUtils.getScreenWidth())
                    .maxHeight(ScreenUtils.getScreenHeight() - 200)
                    .enableDrag(false)
                    .asCustom(new BottomPopupView(getContext()) {
                        @Override
                        protected int getImplLayoutId() {
                            return R.layout.dialog_integral_recharge;
                        }

                        @Override
                        protected void onCreate() {
                            findViewById(R.id.btn_close).setOnClickListener(v -> dismiss());
                            TextView tvRechargeNum = findViewById(R.id.tv_recharge_num);
                            mLinerViewList.add(findViewById(R.id.tv_liner_15));
                            findViewById(R.id.tv_liner_15).setOnClickListener(v -> {
                                handleRechargeView(v.getTag().toString());
                                //
                                tvRechargeNum.setText(v.getTag().toString());
                            });
                            mLinerViewList.add(findViewById(R.id.tv_liner_30));
                            findViewById(R.id.tv_liner_30).setOnClickListener(v -> {
                                handleRechargeView(v.getTag().toString());
                                //
                                tvRechargeNum.setText(v.getTag().toString());
                            });
                            mLinerViewList.add(findViewById(R.id.tv_liner_60));
                            findViewById(R.id.tv_liner_60).setOnClickListener(v -> {
                                handleRechargeView(v.getTag().toString());
                                //
                                tvRechargeNum.setText(v.getTag().toString());
                            });
                            mLinerViewList.add(findViewById(R.id.tv_liner_100));
                            findViewById(R.id.tv_liner_100).setOnClickListener(v -> {
                                handleRechargeView(v.getTag().toString());
                                //
                                tvRechargeNum.setText(v.getTag().toString());
                            });
                            mLinerViewList.add(findViewById(R.id.tv_liner_200));
                            findViewById(R.id.tv_liner_200).setOnClickListener(v -> {
                                handleRechargeView(v.getTag().toString());
                                //
                                tvRechargeNum.setText(v.getTag().toString());
                            });
                            mLinerViewList.add(findViewById(R.id.tv_liner_300));
                            findViewById(R.id.tv_liner_300).setOnClickListener(v -> {
                                handleRechargeView(v.getTag().toString());
                                //
                                tvRechargeNum.setText(v.getTag().toString());
                            });
                            //
                            mTextViewList.add(findViewById(R.id.tv_integral_15));
                            mTextViewList.add(findViewById(R.id.tv_integral_30));
                            mTextViewList.add(findViewById(R.id.tv_integral_60));
                            mTextViewList.add(findViewById(R.id.tv_integral_100));
                            mTextViewList.add(findViewById(R.id.tv_integral_200));
                            mTextViewList.add(findViewById(R.id.tv_integral_300));
                            //
                            mTextViewList2.add(findViewById(R.id.tv_price_15));
                            mTextViewList2.add(findViewById(R.id.tv_price_30));
                            mTextViewList2.add(findViewById(R.id.tv_price_60));
                            mTextViewList2.add(findViewById(R.id.tv_price_100));
                            mTextViewList2.add(findViewById(R.id.tv_price_200));
                            mTextViewList2.add(findViewById(R.id.tv_price_300));
                            //处理支付方式选择
                            //微信支付
                            RadioButton rbWeixin = findViewById(R.id.rb_weixin);
                            //支付宝支付
                            RadioButton rbAlipay = findViewById(R.id.rb_alipay);

                            rbWeixin.setChecked(true);
                            rbWeixin.setOnClickListener(v -> {
                                rbWeixin.setChecked(true);
                                rbAlipay.setChecked(false);
                            });
                            rbAlipay.setOnClickListener(v -> {
                                rbAlipay.setChecked(true);
                                rbWeixin.setChecked(false);
                            });
                           *//* rbWeixin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                @Override
                                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                                    //微信支付
                                    compoundButton.setChecked(true);
                                    rbAlipay.setChecked(false);
                                }
                            });

                            rbAlipay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                @Override
                                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                                    //支付宝支付
                                    compoundButton.setChecked(true);
                                    rbWeixin.setChecked(false);
                                }
                            });*//*
                        }
                    }).show();*/
        }
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private void handleRechargeView(String tag) {
        for(LinearLayout linearLayout:mLinerViewList){
            if(linearLayout.getTag().toString().equals(tag)){
                linearLayout.setBackground(getResources().getDrawable(R.drawable.shape_view_with_border_selected));
            }else{
                linearLayout.setBackground(getResources().getDrawable(R.drawable.shape_view_with_border_normal));
            }
        }
        for(TextView textView:mTextViewList){
            if(textView.getTag().toString().equals(tag)){
                textView.setTextColor(getResources().getColor(R.color.colorPrimary));
            }else{
                textView.setTextColor(getResources().getColor(R.color.black));
            }
        }
        for(TextView textView:mTextViewList2){
            if(textView.getTag().toString().equals(tag)){
                textView.setTextColor(getResources().getColor(R.color.colorPrimary));
            }else{
                textView.setTextColor(getResources().getColor(R.color.black50));
            }
        }
    }


    private void activeLableView(LabelTextView tvLabel) {
        tvLabel.setBackgroundColor(getResources().getColor(R.color.color_label_view_active));
        tvLabel.setTextColor(getResources().getColor(R.color.color_label_view_label_color_active));
        tvLabel.setLabelBackgroundColor(getResources().getColor(R.color.red_back));
        //
        binding.tvJiFengNum.setText(String.format("(消耗%s积分)", tvLabel.getTag().toString()));
        binding.tvRefreshNum.setText(String.format("%s", tvLabel.getTag().toString()));
    }

    private void disableLableView(LabelTextView tvLabel) {
        tvLabel.setBackgroundColor(getResources().getColor(R.color.color_label_view_disable));
        tvLabel.setTextColor(getResources().getColor(R.color.black50));
        tvLabel.setLabelBackgroundColor(getResources().getColor(R.color.color_label_view_label_color_disable));
    }
}
